datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model User {
  id           Int           @id @default(autoincrement())
  login        String
  password     String
  role         Int
  prepodavatel Prepodavatel?
  student      Student?
}

model Otdelenie {
  id         Int          @id @default(autoincrement())
  director   Prepodavatel @relation(fields: [directorId], references: [id])
  directorId Int          @unique
  title      String
  groups Group[]
}

model Group {
  id        Int          @id @default(autoincrement())
  curator   Prepodavatel @relation(fields: [curatorId], references: [id])
  curatorId Int          @unique
  year      Int
  title     String
  otdelenie Otdelenie @relation(fields: [otdelenieId], references: [id])
  otdelenieId Int
  students Student[]
}

model Student {
  id        Int      @id @default(autoincrement())
  user      User?    @relation(fields: [userId], references: [id])
  userId    Int?     @unique
  familiya  String
  imya      String
  otchestvo String
  birthday  DateTime
  contracts Contract[]
  group Group @relation(fields: [groupId], references: [id])
  groupId Int
}

model Prepodavatel {
  id        Int        @id @default(autoincrement())
  user      User?      @relation(fields: [userId], references: [id])
  userId    Int?       @unique
  otdelenie Otdelenie?
  group     Group?
  familiya  String
  imya      String
  otchestvo String
  birthday  DateTime
}

model Room {
  id         Int    @id @default(autoincrement())
  number     String @unique
  level      Int
  countSeats Int
  area Decimal
  items Inventary[]
  contracts Contract[]
}

model InventaryCategory {
  id    Int    @id @default(autoincrement())
  title String
  items Inventary[]
}

model Inventary {
  id       Int @id @default(autoincrement())
  category InventaryCategory @relation(fields: [categoryId], references: [id])
  categoryId Int
  title String
  invNumber String
  room Room? @relation(fields: [roomId], references: [id])
  roomId Int?
  price Decimal
}

model Contract {
  id Int @id @default(autoincrement())
  date DateTime @default(now())
  studentId Int
  student Student @relation(fields: [studentId], references: [id])
  roomId Int
  room Room @relation(fields: [roomId], references: [id])
  price Decimal
  contractStart DateTime
  contractEnd DateTime
  terminatedDate DateTime?
  payments Payment[]
}

model Payment {
    id Int @id @default(autoincrement())
    contractId Int
    contract Contract @relation(fields: [contractId], references: [id])
    date DateTime @default(now())
    amount Decimal
}